#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/1/6 9:38
# @Author  : Beam
# @Site    : 
# @File    : minions.py
# @Software: PyCharm


from django.shortcuts import HttpResponse,render,redirect
from django.views import View  #CBV模式写法
from django.utils.decorators import method_decorator  #非常重要的一个方法，引用装饰器类
from django.views.decorators.csrf import csrf_exempt  #z这样不需要做csrf的验证了，post请求也可以直接过来不验证
import json
from func import authapi
from api import models

class GetMinionsView(View):
	@method_decorator(authapi.api_auth)
	def get(self,request,*args,**kwargs):
		result = json.loads(request.body)
		returnobj = json.loads(result['return'])

		if not result['success']:
			return HttpResponse(json.dumps({'isFalse': False}, ensure_ascii=False))
		# if result['schedule'] == 'cputimes':
		# 	'''
		# 	cputimes对应的是cpu消耗监控指标，更新时间3分钟
		# 	'''
		# 	models.CpuTimes.objects.create(minions_id=result['id'],user=returnobj['user'],nice=returnobj['nice'],
		# 	                               system=returnobj['system'],idle=returnobj['idle'],iowait=returnobj['iowait'],
		# 	                               softirq=returnobj['softirq'],steal=returnobj['steal'],irq=returnobj['irq'],
		# 	                               guest=returnobj['guest'],guest_nice=returnobj['guest_nice'],
		# 	                               cpupercent=returnobj['cpupercent'])
		# 	return HttpResponse(json.dumps({'isFalse':True},ensure_ascii=False))

		if result['schedule'] == 'sysparameters':
			'''
			sysparameters对应系统一些参数指标，更新时间30分钟
			'''
			models.SysParameters.objects.create(minions_id=result['id'], usercount=returnobj['usercount'],
			                                    cpuphysicalcount=returnobj['cpuphysicalcount'],
			                                    cpulogiccount=returnobj['cpulogiccount'], pids=returnobj['pids'],
			                                    nowtime=returnobj['nowtime'],
			                                    cpucurrent=returnobj['cpucurrent'], memtotal=returnobj['memtotal'])
			return HttpResponse(json.dumps({'isFalse':True},ensure_ascii=False))

		if result['schedule'] == 'memeryminions':
			'''
			memeryminions对应内存参数指标，更新时间60秒
			'''
			models.MemeryMinions.objects.create(minions_id=result['id'],
			                               total=returnobj['total'],available=returnobj['available'],
			                               percent=returnobj['percent'], used=returnobj['used'],free=returnobj['free'],
			                               active=returnobj['active'],inactive=returnobj['inactive'],
			                               buffers=returnobj['buffers'],cached=returnobj['cached'],
			                               shared=returnobj['shared'],swap_total=returnobj['swap_total'],
			                               swap_used=returnobj['swap_used'],swap_free=returnobj['swap_free'],
			                               swap_percent=returnobj['swap_percent'],swap_sin=returnobj['swap_sin'],
			                               swap_sout=returnobj['swap_sout'])
			return HttpResponse(json.dumps({'isFalse':True},ensure_ascii=False))

		if result['schedule'] == 'diskio':
			'''
			diskio对应磁盘一些参数指标，更新时间60秒
			'''
			models.DiskIO.objects.create(minions_id=result['id'],
			                            read_count=returnobj['read_count'],write_count=returnobj['write_count'],
			                            read_bytes=returnobj['read_bytes'], write_bytes=returnobj['write_bytes'],
			                            read_time=returnobj['read_time'],write_time=returnobj['write_time'],
			                            all_read_count=returnobj['all_read_count'],
			                            all_write_count=returnobj['all_write_count'],
			                            all_read_bytes=returnobj['all_read_bytes'],
			                            all_write_bytes=returnobj['all_write_bytes'],
			                            all_read_time=returnobj['all_read_time'],
			                             all_write_time=returnobj['all_write_time'])
			return HttpResponse(json.dumps({'isFalse':True},ensure_ascii=False))

		if result['schedule'] == 'networkio':
			'''
			networkio对应网络一些参数指标，更新时间60秒
			'''
			models.NetworkIO.objects.create(minions_id=result['id'],
			                                bytes_sent=returnobj['bytes_sent'],bytes_recv=returnobj['bytes_recv'],
			                                packets_sent=returnobj['packets_sent'], packets_recv=returnobj['packets_recv'],
			                                errin=returnobj['errin'],errout=returnobj['errout'],
			                                dropin=returnobj['dropin'],dropout=returnobj['dropout'],
			                                all_bytes_sent=returnobj['all_bytes_sent'],all_bytes_recv=returnobj['all_bytes_recv'],
			                                all_packets_sent=returnobj['all_packets_sent'],all_packets_recv=returnobj['all_packets_recv'],
			                                all_errin=returnobj['all_errin'],all_errout=returnobj['all_errout'],
			                                all_dropin=returnobj['all_dropin'],all_dropout=returnobj['all_dropout'])
			return HttpResponse(json.dumps({'isFalse':True},ensure_ascii=False))

		if result['schedule'] == 'netstat':
			'''
			netstat对应tcp端口连接一些参数指标，更新时间60秒
			'''
			models.NetStat.objects.create(minions_id=result['id'],
			                             established=returnobj['ESTABLISHED'],last_ack=returnobj['LAST_ACK'],
			                             time_wait=returnobj['TIME_WAIT'], syn_sent=returnobj['SYN_SENT'],
			                             close_wait=returnobj['CLOSE_WAIT'],listen=returnobj['LISTEN'],)
			return HttpResponse(json.dumps({'isFalse':True},ensure_ascii=False))

		if result['schedule'] == 'cpuload':
			'''
			cpuload对应cpu负载一些参数指标，更新时间60秒
			'''
			models.CpuLoad.objects.create(minions_id=result['id'],nowpercent=returnobj['nowpercent'],
			                             loadone=returnobj['loadone'],loadfive=returnobj['loadfive'],
			                              loadfiften=returnobj['loadfiften'])
			return HttpResponse(json.dumps({'isFalse':True},ensure_ascii=False))

	@method_decorator(csrf_exempt)
	def post(self,request,*args,**kwargs):
		#测试
		print(request.body)
		return HttpResponse('post ok')